package com.quartet.modules.vehicle.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quartet.modules.vehicle.dao.TDriverInfoDao;
import com.quartet.modules.vehicle.dto.TDriverInfoAddRequestDto;
import com.quartet.modules.vehicle.entity.TDriverInfo;
import com.quartet.modules.vehicle.service.TDriverInfoService;
import com.quartet.utils.JsonTool;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 驾驶员信息表(TDriverInfo)表服务实现类
 *
 * @author makejava
 * @since 2021-05-04 16:33:13
 */
@Service("tDriverInfoService")
public class TDriverInfoServiceImpl extends ServiceImpl<TDriverInfoDao, TDriverInfo> implements TDriverInfoService {

    /**
     * 添加驾驶员信息
     *
     * @param tDriverInfoAddRequestDto
     */
    @Override
    public void addDriverInfoInfo(TDriverInfoAddRequestDto tDriverInfoAddRequestDto) {
        if (!ObjectUtils.isEmpty(tDriverInfoAddRequestDto)) {
            TDriverInfo tDriverInfo = BeanUtil.copyProperties(tDriverInfoAddRequestDto, TDriverInfo.class);
            tDriverInfo.setDriverPhoto(JsonTool.beanToJson(tDriverInfoAddRequestDto.getDriverPhoto()));
            this.baseMapper.insert(tDriverInfo);
        }
    }

    /**
     * 功能描述: 修改驾驶员信息
     *
     * @param tDriverInfoAddRequestDto
     * @return void
     * @author huipanxing huipanxing@chinasofti.com
     * @date 2021/5/13 16:18
     */
    @Override
    public void updateDriverInfoInfo(TDriverInfoAddRequestDto tDriverInfoAddRequestDto) {
        if (!ObjectUtils.isEmpty(tDriverInfoAddRequestDto)) {
            TDriverInfo tDriverInfo = BeanUtil.copyProperties(tDriverInfoAddRequestDto, TDriverInfo.class);
            tDriverInfo.setDriverPhoto(JsonTool.beanToJson(tDriverInfoAddRequestDto.getDriverPhoto()));
            this.baseMapper.updateById(tDriverInfo);
        }
    }

    /**
     * 根据车辆编码删除驾驶员信息
     *
     * @param vehicleCode
     */
    @Override
    public void deleteDriverInfoInfo(String vehicleCode) {
        this.baseMapper.delete(new QueryWrapper<TDriverInfo>().lambda().eq(TDriverInfo::getVehicleCode, vehicleCode));
    }

    /**
     * 根据车辆编码查询驾驶员信息
     *
     * @param vehicleCode
     * @return
     */
    @Override
    public TDriverInfoAddRequestDto selectDriverInfoInfo(String vehicleCode) {
        TDriverInfo tDriverInfo = this.baseMapper.selectOne(new QueryWrapper<TDriverInfo>().lambda().eq(TDriverInfo::getVehicleCode, vehicleCode));
        TDriverInfoAddRequestDto tDriverInfoAddRequestDto = BeanUtil.copyProperties(tDriverInfo, TDriverInfoAddRequestDto.class);
        if (!ObjectUtils.isEmpty(tDriverInfo.getDriverPhoto())) {
            List<String> driverPhotoList = JSON.parseArray(tDriverInfo.getDriverPhoto().toString(), String.class);
            tDriverInfoAddRequestDto.setDriverPhoto(driverPhotoList);
        }
        return tDriverInfoAddRequestDto;
    }
}
